
import {
  list
} from '../../../../js/modules/mall';
import {
  activities
} from '../../../../js/modules/market';
import {
  pagesInfo
} from '../../../../js/router';
import authorize from '../../../../js/modules/authorize';
import BasePage from '../../../../js/common/base-page'
import { api,storage} from '../../../../js/util'

const app = getApp()
BasePage({
  pageKey: "productCommentList",
  validateLogin: false,
  /**
   * 页面的初始数据
   */
  data: {
    ShowVendorName:false,
    is_search:false,
    marketSource:[],
    hasFooter:false,   //是否有底部导航
    isIphoneX:app.globalData.isIphoneX,
    mainColor:'',//主题色
    // hasFooter:true,   //是否有底部导航
    Currency:'',//货币符号
    showStyle:'line',
    NoLoginShowPrice:false,//
    isLogin:true,//判断是否登录
    CartName:'',
    FieldDisplayName:[],
    ClassDisplayField:{},
    listState:true,
    tabindex:-1,
    hasNav:false,
    cids:0,
    GoodsUnitDisplayMode:0,
    AppQuickTemplateConfig: {}, // 快速下单的目录样式
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.productlist = this.selectComponent("#productlist");
    if(options.keywords!=undefined){
      this.productlist.setData({
        keywords: options.keywords,
        rule_id:options.rule_id||-1,
        cid: options.cid,
        bid: options.bid,
        tag_id: options.tag_id || '',
        typeId:options.types,
        sortField: "",
        sortDirect: "",
      });
      this.productlist.clearSort()
    }
    app.initTabBar();
    //判断是否装修了商品导航，如果装修了，则显示底部
    let hasGoods = app.globalData.tabBar.list.filter((item)=>item.text=='商品');
    if(hasGoods.length!=0){
      this.setData({
        hasFooter:true
      },function(){
        app.initTabBar();
      })
    }
    options.hasnav = (options.hasnav == 'true' || options.hasnav)?true:false;
    this.setData({
      showStyle:storage.get('showStyle') || 'line',
      hasNav:options.hasnav,
      cids:options.cid,
      types:options.types || app.globalData.product_list.typeId
    })
    if(this.data.hasNav){
      this.productlist.loadMain();
      wx.setNavigationBarTitle({
        title: '商品目录',
      })
    } else {
      wx.setNavigationBarTitle({
        title: '商品列表',
      })
    }
    // console.log('---',this.data.showStyle,options,options.hasnav != 'false')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var tabindex = app.getTabBarindex();
    this.setData({
      tabindex:tabindex
    })
    var that=this;
    var p = app.globalData.product_list;
    var rule_id=p.rule_id||-1;
    if (p.bid.length>0){
      this.productlist.setData({
        bid: p.bid
      });
    }
    if (p.cid > 0) {
      this.productlist.setData({
        cid: p.cid,
        selectedCategoryId:p.cid
      });
    }
    if (p.rule_id > 0) {
      this.productlist.setData({
        rule_id: p.rule_id
      }); 
    }
    if (p.tag_id>0){
      this.productlist.setData({
        tag_id: p.tag_id
      });
    }
    if (p.index_search_keyword && p.index_search_keyword.length>0) {
      // 有关键词时重置排序条件
      this.productlist.setData({
        keywords: p.index_search_keyword,
        sortField: "",
        sortDirect: "",
      });
      this.productlist.clearSort()
    }
    app.globalData.product_list.rule_id = -1;
    app.globalData.product_list.index_search_keyword = "";
    app.globalData.product_list.cid = 0;
    app.globalData.product_list.tag_id = 0;
    this.getWholesaleConfig();
    
    if (this.data.is_search) return;
    if (this.productlist.data.rule_id>0){
        activities.getPromoteRuleInfo(this.productlist.data.rule_id,function(content){
          console.log(content,'=======content')
          that.setData({
            hasMaket: true,
            marketSource:content
          });
        });
    }
    this.setData({
      mainColor:api.titleColor||"#e56b14",
      Currency:api.Currency || '￥',
      isLogin:authorize.isLogin('store')
    });
    //获取购物车数量
    //cart.get_cart_count();
  },
  
  /**加载商品类的配置项参数 */
  getWholesaleConfig() {
    var that = this;
    var fkid = app.dns.proprietorId;
    var fkflag = app.dns.proprietor;
    var isLoadList=false;
    list.getWholesaleConfigs(
      fkid,
      fkflag,
      function (content) {
       
        var fieldDisplayName = content.FieldDisplayName?content.FieldDisplayName.split(','):[];
        var mClassDisplayField = content.MClassDisplayField?content.MClassDisplayField.split('|'):[];
        var classDisplayField = {Price:false,ProductCode:false,PackageSpec:false,BuyLimit:false,BarCode:false,PromotLadder:false,Sell:false};
        for(var i = 0;i<mClassDisplayField.length;i++){
          if (mClassDisplayField[i]=='Sell') {
            classDisplayField[mClassDisplayField[i]]=content.ShowProductSells;
          } else {
            classDisplayField[mClassDisplayField[i]]=true;
          }
        }
        that.setData({
          AppQuickTemplateConfig: content.AppQuickTemplateConfig || {},
          BGColorForMobile: content.BGColorForMobile,
          ShowVendorName:content.ShowVendorName
        })
        that.productlist.setData({
          is_search: that.data.is_search,
          quantityDigits: content.QuantityDigits,
          priceDigits: content.PriceDigits,
          enableZeroStockBuy: content.EnableZeroStockBuy,
          NotShowProductNoneOffered: content.NotShowProductNoneOffered, // 是否不显示无货商品按钮
          showRetailPrice: content.ShowRetailPrice,
          spectStyle: content.GoodsDisplayMode,
          addressAssignDepot: content.AddressAssignDepot,
          isScanty: content.IsScanty, //是否允许缺货登记
          NoLoginShowPrice:content.NoLoginShowPrice,//0：建议零售价 1：不显示价格
          CartName:api.CartName || '购物车',
          FieldDisplayName:fieldDisplayName,
          ClassDisplayField:classDisplayField,
          GoodsUnitDisplayMode:content.GoodsUnitDisplayMode,
          FrontShowStockDetail:content.FrontShowStockDetail,
          BGColorForMobile: content.BGColorForMobile
        },function(){
          if (!isLoadList){
            isLoadList=true;
            that.productlist.loadMain();
          }
        });
       
      });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
    this.productlist.onPullDownRefresh();

  },
  onPageScroll: function (e) {
    if (!this.productlist) return;
    if (this.scroll_timeout) {
      clearTimeout(this.scroll_timeout);
    }
    let scrollTop = e.scrollTop;
    this.scroll_timeout = setTimeout(()=>{
      this.productlist.setScollTop(scrollTop)
    }, 50);
  },
  /**
   * 页面滚动到
   */
  onPageScrollTo(e){
    wx.pageScrollTo({
      scrollTop: e.detail.scrollTop,
      duration: 0
    })  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.productlist.onReachBottom();
  },

 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let title = this.productlist.data.keywords||"";
    let rule_id = this.productlist.data.rule_id;
    let cid = this.productlist.data.cid;      //分类
    let bid = this.productlist.data.bid;      //品牌
    let tag_id = this.productlist.data.tag_id // 标签id
    var storeInfo = authorize.getEntity();
   
    let scene="";
    if(storeInfo){
      scene=storeInfo.fkFlag+"_"+storeInfo.fkId+"_"+storeInfo.manager.ManagerId;
    } 

    let path = pagesInfo.productList.url + `?keywords=${title}&bid=${bid}&cid=${cid}&rule_id=${rule_id}&tag_id=${tag_id}&scene=${scene}`;
    return {
      title: title||"商品列表",
      path: path
    }
  },
  /**
  * 生命周期函数--监听页面隐藏
  */
  onHide: function () {
    this.data.is_search = true;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },
  preventTouchMove(){
    
  }
})